package com.example.mytayunapi.Controller;

import com.example.mytayunapi.Service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/upload")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/aureole")
    public ResponseEntity<Map<String, String>> uploadAureole(@RequestParam("avatar") MultipartFile avatar,
                                                             @RequestParam("userId") String userId) {
        try {
            // 调用Service层方法处理文件上传
            String newAvatarPath = fileService.saveAvatar(avatar, userId);
            Map<String, String> response = new HashMap<>();
            response.put("newAvatarPath", newAvatarPath);
            return ResponseEntity.ok(response);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @PostMapping("/shareole")
    public ResponseEntity<Map<String, String>> uploadShareole(@RequestParam("avatar") MultipartFile avatar,
                                                              @RequestParam("School_degree") String School_degree,
                                                              @RequestParam("Share_title") String Share_title,
                                                              @RequestParam("category") String category,
                                                              @RequestParam("Share_content") String Share_content
                                                             ){
        try {
            // 调用Service层方法处理文件上传
            String newAvatarPath = fileService.saveAvatar2(avatar, School_degree,Share_title,category,Share_content);
            Map<String, String> response = new HashMap<>();
            response.put("newAvatarPath", newAvatarPath);
            return ResponseEntity.ok(response);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}

